package com.yikes.srb.controller.web;

import com.alibaba.fastjson2.JSONObject;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.constant.hfb.RequestHelper;
import com.yikes.srb.leadingModle.form.userAccount.UserAccountAddForm;
import com.yikes.srb.leadingModle.form.userAccount.UserAccountEditForm;
import com.yikes.srb.leadingModle.form.userAccount.UserAccountQuery;
import com.yikes.srb.leadingModle.vo.UserAccountVO;
import com.yikes.srb.service.UserAccountService;
import com.yikes.srb.web.utils.jwt.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@Slf4j
@RequiredArgsConstructor
@Tag(name = "UserAccountController", description = "用户账户")
@RestController
@RequestMapping("/admin/user_account")
public class UserAccountController {

    private final UserAccountService service;


    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<UserAccountVO>> listPage(UserAccountQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<UserAccountVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody UserAccountAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody UserAccountEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "充值")
    @PostMapping("/auth/commitCharge/{chargeAmt}")
    public Result commitCharge(@PathVariable BigDecimal chargeAmt, HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        String formStr = service.commitCharge(chargeAmt, userId);
        return Result.ok(formStr);
    }

    @Operation(description = "用户充值异步回调")
    @PostMapping("/notify")
    public String notify(HttpServletRequest request) {
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("用户充值异步回调：" + JSONObject.toJSONString(paramMap));

        //校验签名
        if(RequestHelper.isSignEquals(paramMap)) {
            //充值成功交易
            if("0001".equals(paramMap.get("resultCode"))) {

                return service.notify(paramMap);
            } else {

                log.info("用户充值异步回调充值失败：" + JSONObject.toJSONString(paramMap));
                return "success";
            }
        } else {

            log.info("用户充值异步回调签名错误：" + JSONObject.toJSONString(paramMap));
            return "fail";
        }
    }

    @Operation(description = "查询账户余额")
    @GetMapping("/auth/getAccount")
    public Result getAccount(HttpServletRequest request){

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        BigDecimal account = service.getAccount(userId);
        return Result.ok(account);
    }

    @Operation(description = "用户提现")
    @PostMapping("/auth/commitWithdraw/{fetchAmt}")
    public Result commitWithdraw(
            @Parameter(name = "金额", required = true)
            @PathVariable BigDecimal fetchAmt, HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        String formStr = service.commitWithdraw(fetchAmt, userId);
        return Result.ok(formStr);
    }

    @Operation(description = "用户提现异步回调")
    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest request) {
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("提现异步回调：" + JSONObject.toJSONString(paramMap));

        //校验签名
        if(RequestHelper.isSignEquals(paramMap)) {
            //提现成功交易
            if("0001".equals(paramMap.get("resultCode"))) {

                service.notifyWithdraw(paramMap);
            } else {

                log.info("提现异步回调充值失败：" + JSONObject.toJSONString(paramMap));
                return "fail";
            }
        } else {

            log.info("提现异步回调签名错误：" + JSONObject.toJSONString(paramMap));
            return "fail";
        }
        return "success";
    }

}
